package com.jiangfeixiang.springbootswgger2api.controller;

import com.jiangfeixiang.springbootswgger2api.entity.User;
import com.jiangfeixiang.springbootswgger2api.service.UserService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by jiangfeixiang on 2018/8/28
 * @author smfx1314
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {
    /**
     * 注入userService
     */
    @Autowired
    private UserService userService;

    /**
     * 查询所有用户
     */
    /*@ApiOperation(value="获取用户列表")
    @RequestMapping(value = "/user",method = RequestMethod.GET)
    public List<User> getListAll(){
        List<User> listAll = userService.getAllUser();
        return listAll;
    }*/

    @ApiOperation(value="获取用户列表")
    @RequestMapping(value = "/getUser",method = RequestMethod.GET)
    public String getListAll(Model model){
        List<User> listAll = userService.getAllUser();
        model.addAttribute("hello","你好世界");
        model.addAttribute("listAll", listAll);
        return "success";
    }


    /**
     * 用户保存
     * @return
     */
    @ApiOperation(value = "添加用户",notes = "根据user添加用户")
    @ApiImplicitParam(name = "user",value = "用户user",required = true,dataType = "User")
    @RequestMapping(value = "/users",method = RequestMethod.POST)
    public String saveUser(@RequestBody User user){
        userService.saveUser(user);
        return "success";
    }

    /**
     * 根据id查询
     */
    @ApiOperation(value = "根据id查询")
    @ApiImplicitParam(name = "id",value = "用户id")
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public User getById(@PathVariable("id") Integer id){
        User user = userService.getById(id);
        return user;
    }

    /**
     * 校验用户名
     * @param username
     * @return
     */
    @ApiOperation(value = "校验用户名")
    @ApiImplicitParam(name = "userName",value = "用户名",required = true,dataType = "String")
    @RequestMapping(value = "/{username}",method = RequestMethod.POST)
    public Boolean checkUserName(@PathVariable("username")String username){
        Boolean aboolean = userService.checkUserName(username);
        if (aboolean){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 测试根据id查询
     */
    @RequestMapping("/getById2/{id}")
    @ResponseBody
    public User getById2(@PathVariable("id") Integer id){
        User user = userService.getById(id);
        return user;
    }

    /**
     * 修改用户
     * @param user
     */
    @ApiOperation(value = "修改用户")
    @ApiImplicitParam(name = "user",value = "用户",required = true,dataType = "User")
    @RequestMapping(value = "/user",method = RequestMethod.PUT)
    public String updateUser(@RequestBody User user){
        userService.updateUser(user);
        return "success";
    }

    /**
     * 根据id删除用户
     */
    @ApiOperation(value = "根据id删除用户")
    @ApiImplicitParam(name = "id",value = "用户id",required = true,dataType = "Integer")
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public String deleteUser(@PathVariable Integer id){
        userService.deleteUser(id);
        return "success";
    }
}